package com.yc.shopping.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;

import java.sql.SQLException;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.shopping.dao.AdminDao;
import com.yc.shopping.util.ShoppingUtil;

import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class LoginUI {

	protected Shell shell;
	private Text text_aname;
	private Text text_pwd;
	static String username;
	static String usertype;
	AdminDao dao = new AdminDao();

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			LoginUI window = new LoginUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		shell.setImage(SWTResourceManager.getImage("D:\\eclipse\\Project\\YC123_Project\\src\\com\\yc\\shopping\\image\\yclogo.png"));
		shell.setSize(450, 300);
		shell.setText("收银系统登录");
		
		//窗口居中
		ShoppingUtil.centerWindows(shell);
		Label label = new Label(shell, SWT.NONE);
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label.setImage(null);
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label.setText("登录界面");
		label.setBounds(152, 26, 80, 25);
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		label_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label_1.setBounds(44, 76, 69, 25);
		label_1.setText(" 用户名 ：");
		
		Label label_2 = new Label(shell, SWT.NONE);
		label_2.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		label_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label_2.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		label_2.setBounds(44, 131, 69, 25);
		label_2.setText(" 密   码 ：");
		
		text_aname = new Text(shell, SWT.BORDER);
		text_aname.setBounds(122, 76, 173, 23);
		
		text_pwd = new Text(shell, SWT.BORDER | SWT.PASSWORD);
		text_pwd.setBounds(122, 133, 173, 23);
		
		Button btn_login = new Button(shell, SWT.NONE);
		//管理员登录界面
		btn_login.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				//获取用户名和密码
				String aname = text_aname.getText().trim();
				String pwd = text_pwd.getText().trim();
				//调用Admin中的login方法
				try {
					Map<String, Object> map = dao.login(aname, pwd);
					//非空判断
					if(map==null) {
						//登陆失败
						//弹框提示用户名或密码输入错误
						ShoppingUtil.showMessage(shell, "提示:", "用户名或密码错误");
						return ;
					}
					String type = map.get("ATYPE").toString();
					username=aname;
					usertype=type;

					if("收银员".equals(type)) {
						//跳转收银界面
						//存储登录信息
						ShoppingUtil.adminLogin=map;
						SaleMainUI saleMainUI = new SaleMainUI();
						shell.dispose();
						saleMainUI.open();
					}
					if("超级管理员".equals(type)||"管理员".equals(type)) {
						//跳转主界面
						//存储登录信息
						ShoppingUtil.adminLogin=map;
						AdminMainUI ui = new AdminMainUI();
						shell.dispose();
						ui.open();
					}
					
					//登录成功		
					
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				
			}
		});
		btn_login.setBounds(82, 188, 80, 27);
		btn_login.setText("登录");
		
		Button btn_exit = new Button(shell, SWT.NONE);
		btn_exit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		btn_exit.setBounds(231, 188, 80, 27);
		btn_exit.setText("退出系统");

	}
}
